package com.yl.applicationsecurity.controller;

import com.yl.applicationsecurity.pojo.Result;
import com.yl.applicationsecurity.pojo.User;
import com.yl.applicationsecurity.service.UserService;
import com.yl.applicationsecurity.utils.JwtUtil;
import com.yl.applicationsecurity.utils.Md5Util;
import com.yl.applicationsecurity.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService UserService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    public Result login(String username, String password){
        //判断密码是否正确
        User user = UserService.findByUsername(username);
        if(null!=user&&user.getPassword().equals(Md5Util.getMD5String(password))){

            Map<String,Object> claims = new HashMap<>();
            claims.put("id",user.getId());
            claims.put("username",username);

            String token = JwtUtil.genToken(claims);

            //把token存储到redis中
            ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
            operations.set(token,token,4,TimeUnit.HOURS);

            return  Result.success(token);
        }

        return  Result.error("用户名或密码错误");
    }


    @GetMapping("/loginOut")
    public Result loginOut(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        stringRedisTemplate.delete(token);
        return  Result.success("退出登录成功");

    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Map<String,Object> map =  ThreadLocalUtil.get();
        User user = UserService.findByUsername((String)map.get("username"));
        return  Result.success(user);

    }


    @PostMapping("/updatePwd")
    public Result updatePwd(HttpServletRequest request,String oldPwd,String newPwd) {
        Map<String,Object> map =  ThreadLocalUtil.get();
        User user = UserService.findByUsername((String)map.get("username"));
        if(user.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            UserService.updatePwd(user.getUsername(), oldPwd, newPwd);
            String token = request.getHeader("Authorization");
            stringRedisTemplate.delete(token);
            return  Result.success("修改成功");
        }else{
            return  Result.error("旧密码输入不正确");
        }
    }



}
